/*
 * @Author: jiu yin
 * @Date: 2023-05-15 14:36:28
 * @LastEditTime: 2023-06-07 17:04:18
 * @LastEditors: jiu yin zhen jing
 * @FilePath: \AI admin\src\redux\index.ts
 * jiu
 */
import { configureStore, combineReducers } from "@reduxjs/toolkit";
import {
   persistStore,
   persistReducer,
   FLUSH,
   REHYDRATE,
   PAUSE,
   PERSIST,
   PURGE,
   REGISTER,
} from "redux-persist";
import sessionStorage from "redux-persist/lib/storage/session";

import keepActive from "@/redux/modules/keepActive";
import common from "@/redux/modules/common";
import asyncRouter from "@/redux/modules/asyncRouter";
import user from "@/redux/modules/user";
// 创建reducer(拆分reducer)
const reducer = combineReducers({
   asyncRouter,
   keepActive,
   common,
   user,
});

// redux 持久化配置
const persistConfig = {
   key: "redux-state",
   storage: sessionStorage,
};
const persistReducerConfig = persistReducer(persistConfig, reducer);

export const store = configureStore({
   reducer: persistReducerConfig,
   //序列化 不然报错
   middleware: getDefaultMiddleware =>
      getDefaultMiddleware({
         serializableCheck: {
            ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
         },
      }),
});

export const persistor = persistStore(store);

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch;
